package et.item;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.item.Item;

import java.lang.reflect.Field;

/**
 * Created by winston_wang on 2017/6/3.
 */
public class ETItemManager {
    public ETItemManager(String mods[]){
        initItemRegistry(mods);
    }
    private void initItemRegistry(String mods[]){
        Class researchClass = ETItems.class;
        for(Field field : researchClass.getDeclaredFields()){
            if (field.isAnnotationPresent(ETItem.class)){
                ETItem etItem = field.getAnnotation(ETItem.class);
                if(etItem.dependMods().length!=0) {
                    if(etItem.dependMods().length==mods.length) {
                        int index = 0;
                        for (String modid : etItem.dependMods()) {
                            for(String checkid : mods){
                                if (!modid.contains(checkid)) {
                                    continue;
                                }
                            }
                            index++;
                        }
                        try {
                            if(field.get(null) instanceof Item){
                                GameRegistry.registerItem((Item)field.get(null),etItem.registerName());
                            }else if (field.get(null) instanceof Block){
                                GameRegistry.registerBlock((Block)field.get(null),etItem.registerName());
                            }else {
                                FMLCommonHandler.instance().exitJava(-125,true);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                            FMLCommonHandler.instance().exitJava(-125,true);
                        }
                    }
                }else {
                    try {
                        if(field.get(null) instanceof Item){
                            GameRegistry.registerItem((Item)field.get(null),etItem.registerName());
                        }else if (field.get(null) instanceof Block){
                            GameRegistry.registerBlock((Block)field.get(null),etItem.registerName());
                        }else {
                            FMLCommonHandler.instance().exitJava(-125,true);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        FMLCommonHandler.instance().exitJava(-125,true);
                    }
                }
            }
        }
    }
    public void initTexture(){
        Class researchClass = ETItems.class;
        for(Field field : researchClass.getDeclaredFields()){
            if (field.isAnnotationPresent(ETItem.class)){
                ETItem etItem = field.getAnnotation(ETItem.class);
                try {
                    if(!etItem.texture().equals("")){
                        if(field.get(null) instanceof Item){
                            ((Item)field.get(null)).setTextureName(etItem.texture());
                        }else if (field.get(null) instanceof Block){
                            ((Block)field.get(null)).setBlockTextureName(etItem.texture());
                        }else {
                            FMLCommonHandler.instance().exitJava(-125,true);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    FMLCommonHandler.instance().exitJava(-125,true);
                }
            }
        }
    }
}
